package com.ltv.xiangqibook.web;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

/**
 * This class defines the methods for basic operations of create, update & retrieve
 * for the Game entity
 * 
 * @author
 *
 */
public class Game {

	/**
	 * Create the game
	 * 
	 * @param id
	 *          : id of the game
	 * @param location
	 *          : location of the game
	 * @param comment
	 *          : comment
	 * 
	 * @return persisted game
	 */
	public static void createGame(String id, String location, Text comment) {
		Entity game = new Entity("games", id);

		game.setProperty("location", location);
		game.setProperty("comment", comment);

		Util.persistEntity(game);
	}

	/**
	 * Return all the games
	 * 
	 * @return games
	 */
	public static Iterable<Entity> getAllGames() {
		return Util.listEntities("games", null, null);
	}

	/**
	 * Get game entity
	 * 
	 * @param id
	 *          : id of the game
	 * @return: game entity
	 */
	public static Entity getGame(String id) {
		Key key = KeyFactory.createKey("games", id);
		return Util.findEntity(key);
	}

}
